let arrayToList = (arr) => {
    let list = null;
    let nowlist = null;
    for (let i = 0; i < arr.length; i++) {
        if (i === 0) {
            list = {
                value: arr[i],
                rest: null
            }
            nowlist = list
        } else {
            nowlist.rest = {
                value: arr[i],
                rest: null
            }
            nowlist = nowlist.rest
        }

    }
    return list;
}


let listToArray = (list) => {
    let arr = []
    let nowList = list
    while (nowList != null) {
        arr.push(nowList.value)
        nowList = nowList.rest
    }
    return arr;
}


let prepend = (val, list) => {
    return res = {
        value: val,
        rest: list
    }
}

let index = 0
let nth = (list, idx) => {
    if (list == null)
        return undefined;
    let i = 0
    while (list != null) {
        if (i == idx) {
            return list.value
        }
        list = list.rest
        i++
    }

}

// Your code here.

console.log(arrayToList([10, 20]));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(listToArray(arrayToList([10, 20, 30])));
// → [10, 20, 30]
console.log(prepend(10, prepend(20, null)));
// → {value: 10, rest: {value: 20, rest: null}}
console.log(nth(arrayToList([10, 20, 30]), 1));
// → 20